package com.hiultra.archive.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.ArchiveBoxStatus;
import com.hiultra.archive.domain.vo.ArchiveBoxStatusRecordsReportVo;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.archive.mapper.ArchiveBoxStatusMapper;
import com.hiultra.archive.service.IArchiveBoxStatusService;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.constant.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 盒状态记录Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveBoxStatusServiceImpl implements IArchiveBoxStatusService {
    @Autowired
    private ArchiveBoxStatusMapper archiveBoxStatusMapper;
    @Value("${orgNo}")
    private String orgNo;
    @Value("${roomNo}")
    private String roomNo;
    @Value("${pubKey}")
    private String pubKey;

    @Value("${priKey}")
    private String priKey;
    @Value("${urlIp}")
    private String urlIp;
    @Value("${archiveBoxStatusReportUrl}")
    private String archiveBoxStatusReportUrl;

    /**
     * 查询盒状态记录
     *
     * @param id 盒状态记录主键
     * @return 盒状态记录
     */
    @Override
    public ArchiveBoxStatus selectArchiveBoxStatusById(Long id) {
        return archiveBoxStatusMapper.selectArchiveBoxStatusById(id);
    }

    /**
     * 查询盒状态记录列表
     *
     * @param archiveBoxStatus 盒状态记录
     * @return 盒状态记录
     */
    @Override
    public List<ArchiveBoxStatus> selectArchiveBoxStatusList(ArchiveBoxStatus archiveBoxStatus) {
        return archiveBoxStatusMapper.selectArchiveBoxStatusList(archiveBoxStatus);
    }

    /**
     * 新增盒状态记录
     *
     * @param archiveBoxStatus 盒状态记录
     * @return 结果
     */
    @Override
    public int insertArchiveBoxStatus(ArchiveBoxStatus archiveBoxStatus) {
        return archiveBoxStatusMapper.insertArchiveBoxStatus(archiveBoxStatus);
    }

    /**
     * 修改盒状态记录
     *
     * @param archiveBoxStatus 盒状态记录
     * @return 结果
     */
    @Override
    public int updateArchiveBoxStatus(ArchiveBoxStatus archiveBoxStatus) {
        return archiveBoxStatusMapper.updateArchiveBoxStatus(archiveBoxStatus);
    }

    /**
     * 批量删除盒状态记录
     *
     * @param ids 需要删除的盒状态记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxStatusByIds(Long[] ids) {
        return archiveBoxStatusMapper.deleteArchiveBoxStatusByIds(ids);
    }

    /**
     * 删除盒状态记录信息
     *
     * @param id 盒状态记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxStatusById(Long id) {
        return archiveBoxStatusMapper.deleteArchiveBoxStatusById(id);
    }

    /**
     * 档案室档案盒状态上报接口
     *
     * @param archiveBoxStatus 盒状态记录
     * @return 结果
     */
    @Transactional
    @Override
    public int reportArchiveBoxStatus(ArchiveBoxStatus archiveBoxStatus) {
        ArchiveBoxStatusRecordsReportVo archiveBoxStatusRecordsVo =new ArchiveBoxStatusRecordsReportVo();
        archiveBoxStatusRecordsVo.setOrgNo(orgNo);
        archiveBoxStatusRecordsVo.setHouseNo("");
        archiveBoxStatusRecordsVo.setRoomNo(roomNo);
        List<ArchiveBoxStatusRecordsReportVo.boxList> boxList = new ArrayList<>();
        ArchiveBoxStatusRecordsReportVo.boxList boxStaus = ToolUtils.convertEntity(archiveBoxStatus, ArchiveBoxStatusRecordsReportVo.boxList::new);
        if (ObjectUtil.isNotNull(archiveBoxStatus.getRecordTime())) {
            boxStaus.setRecordTime(DateUtil.format(archiveBoxStatus.getRecordTime(), "yyyyMMddHHmmss"));
        }
        boxList.add(boxStaus);

        archiveBoxStatusRecordsVo.setBoxStatusList(boxList);
        // 将JsonData对象转换为JSON字符串
        String jsonString = "";
        try {
            jsonString = new ObjectMapper().writeValueAsString(archiveBoxStatusRecordsVo);
            System.out.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //发送请求获取数据
        HashMap hashMap = ToolUtils.sendPostRequest(BusinessConstants.ARCHIVE_BOX_STATUS_REPORT_NO, urlIp, archiveBoxStatusReportUrl, "档案室档案盒状态上报", jsonString, pubKey, priKey);
        System.out.println("档案室档案盒状态上报接口Url"+archiveBoxStatusReportUrl);
        System.out.println("档案室档案盒状态上报接口" + hashMap);
        if (hashMap.get("code").equals("00000")) {
            archiveBoxStatus.setPushStatus(BusinessConstants.END_PUSHSTATUS);
            archiveBoxStatusMapper.updateArchiveBoxStatus(archiveBoxStatus);
        } else {
            System.out.println("档案室档案盒状态上报失败");
        }
        return 0;
    }

    /**
     * 档案室档案盒状态定时上报接口
     *
     * @return 结果
     */
    @Override
    public ResultEntity timingArchiveBoxStatusResultReport() {
        List<ArchiveBoxStatus> archiveBoxStatusList = archiveBoxStatusMapper.selectArchiveBoxStatusList(new ArchiveBoxStatus() {{
            setPushStatus(BusinessConstants.PUSHSTATUS);
        }});

        if (archiveBoxStatusList.size() > 0) {
            for (ArchiveBoxStatus archiveBoxStatus : archiveBoxStatusList) {
                reportArchiveBoxStatus(archiveBoxStatus);
            }
        }
        return ResultEntity.success();
    }

}
